package org.example.studyonline.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.studyonline.mapper.ChapterMapper;
import org.example.studyonline.mapper.CourseMaterialMapper;
import org.example.studyonline.pojo.Chapter;
import org.example.studyonline.pojo.CourseMaterial;
import org.example.studyonline.pojo.DTO.AddChapterDTO;
import org.example.studyonline.pojo.PageBean;
import org.example.studyonline.pojo.VO.CourseMaterialListVO;
import org.example.studyonline.pojo.VO.WatchCountVO;
import org.example.studyonline.service.ChapterService;
import org.example.studyonline.service.CourseMaterialService;
import org.example.studyonline.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Resource
    private CourseMaterialMapper courseMaterialMapper;

    @Resource
    private ChapterMapper chapterMapper;
    @Override
    public Map getMaterials(Integer chapterID) {
        LambdaQueryWrapper<CourseMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CourseMaterial::getChapterID,chapterID);
        List<CourseMaterial> courseMaterials = courseMaterialMapper.selectList(lambdaQueryWrapper);
        List<CourseMaterialListVO> courseMaterialListVOS = BeanCopyUtils.copyBeanList(courseMaterials, CourseMaterialListVO.class);
        HashMap<String, List<CourseMaterialListVO>> map = new HashMap<>();
        map.put("materials",courseMaterialListVOS);
        return map;
    }

    @Override
    public void add(AddChapterDTO addChapterDTO) {
        Chapter chapter = BeanCopyUtils.copyBean(addChapterDTO, Chapter.class);
        chapterMapper.insert(chapter);
    }

    @Override
    public PageBean<WatchCountVO> getWatchCount(Long pageNo, Long pageSize) {
        Long offset = (pageNo - 1) * pageSize;
        List<WatchCountVO> list = chapterMapper.getWatchCountByPage(offset,pageSize);
        Long total = chapterMapper.getWatchCount();
        PageBean<WatchCountVO> pageBean = new PageBean<>();
        pageBean.setPage(pageNo);
        pageBean.setTotal(total);
        pageBean.setItems(list);
        return pageBean;
    }
}
